package com.atguigu.tingshu;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.constant.RedisConstant;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author xiongkunYang
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceAlbumApplication implements CommandLineRunner {


    public static void main(String[] args) {
        SpringApplication.run(ServiceAlbumApplication.class, args);
    }

    private final RedissonClient redissonClient;
    private final AlbumInfoService albuminfoService;

    public ServiceAlbumApplication(RedissonClient redissonClient, AlbumInfoService albuminfoService) {
        this.redissonClient = redissonClient;
        this.albuminfoService = albuminfoService;
    }

    /**
     * 项目启动时，初始化布隆过滤器
     */
    @Override
    public void run(String... args) {
        // 创建布隆过滤器
        RBloomFilter<Long> bloomFilter = this.redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        // 设置布隆过滤器大小，误判率
        bloomFilter.tryInit(100000L, 0.01);

//        List<AlbumInfo> albumInfoList = this.albuminfoService.list(new LambdaQueryWrapper<AlbumInfo>()
//                .eq(AlbumInfo::getIsOpen, 1)
//                .select(AlbumInfo::getId));
//        albumInfoList.forEach(albumInfo -> bloomFilter.add(albumInfo.getId()));

    }

}
